#############################################################################
#   Copyright 2013 Stacy Doss
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#############################################################################

# The following environment variables must be set for this makefile to operate properly
#  CODE_BASE  : The project root directory
#  BITS       : 32 or 64


# C compiler options
BITS			  = 32
CC                = g++
C-COMPILE-OPTIONS = -c -g -fPIC -D_LINUX 
C-LINK-OPTIONS    = -march=i686 -Wimplicit-function-declaration -shared -m$(BITS)
#C-LINK-LIBRARIES  = -lpthread -LC:\Tcl\lib
C-LINK-LIBRARIES  = -lpthread -ltcl84
C-INCLUDE-LIST    = -I./ -IC:\Tcl\include
C-DEFINES         = -D_LINUX
#CFLAGS = -DCPLUSPLUS -g  # for use with C++ if file ext is .c
CFLAGS = 

TMPDIR	 := tmp/

TARGETS  := SpefTcl.so SpefTcl.dll spefparse

OFILES   := SpefTcl.o Spef.o SpefPort.o SpefInterface.o  SpefParValue.o

DIR	      := .
DEPS	  = $(OFILES:%.o=%.d)

all:
	make $(TARGETS)

SpefTcl.so: $(OFILES) SpefParserBase.yy.o SpefParserBase.tab.o 
	g++ $(C-LINK-OPTIONS) -o $@ $(OFILES) SpefParserBase.yy.o SpefParserBase.tab.o $(C-LINK-LIBRARIES)

SpefTcl.dll: $(OFILES) SpefParserBase.yy.o SpefParserBase.tab.o 
	g++ $(C-LINK-OPTIONS) -o $@ $(OFILES) SpefParserBase.yy.o SpefParserBase.tab.o $(C-LINK-LIBRARIES)


SpefInterface.cxx: SpefInterface.i
	swig -tcl -namespace -prefix spef -c++ -o $@ $<

spefparse: SpefParserBase.yy.o SpefParserBase.tab.o Spef.o SpefPort.o SpefParValue.o
	g++ -o $(CFLAGS) spefparse $^

SpefParserBase.tab.h: SpefParserBase.y SpefParserBase.l
	bison -d SpefParserBase.y

SpefParserBase.tab.c: SpefParserBase.y SpefParserBase.y
	bison -d SpefParserBase.y

SpefParserBase.yy.c: SpefParserBase.y SpefParserBase.l SpefParserBase.tab.h
	flex -o SpefParserBase.yy.c SpefParserBase.l

%.o: %.cxx
	g++ $(C-COMPILE-OPTIONS) -I./ $(C-DEFINES) $(C-INCLUDE-LIST) $<

%.o: %.cpp
	g++ $(C-COMPILE-OPTIONS) -I./ $(C-DEFINES) $(C-INCLUDE-LIST) $<

%.d: %.cpp
	@echo Generating dependency file $@ for $<
	@rm -f $@
	@set -e; $(CXX) -MM $(C-COMPILE-OPTIONS) -I./ $(C-DEFINES) $(C-INCLUDE-LIST) $< > $@; \
	[ -s $@ ] || rm -f $@

%.d: %.cxx
	@echo Generating dependency file $@ for $<
	@rm -f $@
	@set -e; $(CXX) -MM $(C-COMPILE-OPTIONS) -I./ $(C-DEFINES) $(C-INCLUDE-LIST) $< > $@; \
	[ -s $@ ] || rm -f $@

include	$(DEPS)

clean:
	rm -f *.o spefparse.exe SpefParserBase.yy.[ch] SpefParserBase.tab.[ch] *.o *.so *~ *.d *.cxx
